import { Button, Tooltip } from 'antd';
import { Fragment } from 'react';
import { EditOutlined, CheckCircleOutlined, StopOutlined, DeleteOutlined, FileDoneOutlined } from '@ant-design/icons';
import { getColorTextLoadMap, getColorTextLoadMapDom, getFileNameByPath, getTenantID, newGetFileNameNotType, removeDateHmsStr, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { ILoadMapTypeAll } from '@/common';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ManageCommonAPI } from '@/pages/type';
import MyIcon from '@/components/MyIcon';

// 操作按钮
const Operation = (curRes: Record<string, any>, clickFnc: TempContractManageAPI.IOperationClick, record: Record<string, any>) => {
  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    // {
    //   icon: <EyeOutlined />,
    //   btnName: '查看',
    //   clickFnc: clickFnc,
    //   clickType: 'onView',
    //   isVisible: curRes?.['/contractTemplateController/getMcontractTemplateByID'] && record?.id && record?.templateURL,
    // },
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: clickFnc,
      clickType: 'onEdit',
      isVisible:
        curRes?.['/contractTemplateController/edit'] &&
        record?.id &&
        (record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1000` ||
          record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1001`),
    },
    {
      icon: <CheckCircleOutlined />,
      btnName: '发布',
      clickFnc: clickFnc,
      clickType: 'onSign',
      isVisible:
        curRes['/contractTemplateController/sign'] &&
        record?.id &&
        (record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1000` ||
          record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1001`),
    },
    {
      icon: <FileDoneOutlined />,
      btnName: '修订',
      clickFnc: clickFnc,
      clickType: 'onAmend',
      isVisible: curRes['/contractTemplateController/revise'] && record?.id && record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1002`,
    },
    {
      icon: <StopOutlined style={{ color: 'red' }} />,
      btnName: <span style={{ color: 'red' }}>作废</span>,
      clickFnc: clickFnc,
      clickType: 'onCancel',
      isVisible:
        curRes['/contractTemplateController/cancel'] &&
        record?.id &&
        (record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1001` ||
          record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1002`),
    },
    {
      icon: <DeleteOutlined style={{ color: 'red' }} />,
      btnName: <span style={{ color: 'red' }}>删除</span>,
      clickFnc: clickFnc,
      clickType: 'onDelete',
      isVisible: curRes['/contractTemplateController/delete'] && record?.id && record?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1000`,
    },
  ];

  return tableMoreBtnConfig(otherBtnConfig, record);
};

const getTableColumns = (curRes: Record<string, any>, clickFnc: TempContractManageAPI.IOperationClick) => {
  return [
    {
      title: '范本名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 230,
      visible: true,
      fixed: 'left',
      unDrag: true,
      render: (text: string | null) => {
        return (
          <Tooltip title={text ? tooltipLength(text, 50) : ''}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '范本文本',
      dataIndex: 'templateURL',
      key: 'templateURL',
      width: 250,
      visible: true,
      render: (text: string | null, record: Record<string, any>) => {
        const fileName: string = getFileNameByPath(text);
        return (
          <>
            {text ? (
              <Tooltip title={fileName}>
                <a
                  onClick={() => {
                    clickFnc?.onNtko(record);
                  }}
                >
                  <MyIcon type="icon-file-text-fill" />
                  &nbsp;
                  {newGetFileNameNotType(fileName, 7)}
                </a>
              </Tooltip>
            ) : (
              <>--</>
            )}
          </>
        );

        //         return (
        //   <Tooltip title={text ? tooltipLength(text, 50) : ''}>
        //     <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
        //   </Tooltip>
        // );
      },
    },
    {
      title: '范本描述',
      dataIndex: 'templateInfo',
      key: 'templateInfo',
      width: 150,
      visible: true,
      render: (text: string | null, record: Record<string, any>) => {
        // return (
        //   <Tooltip title={text ? tooltipLength(text, 50) : ''}>
        //     <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
        //   </Tooltip>
        // );
        return (
          <a
            style={{ color: '#ea0000' }}
            onClick={() => {
              clickFnc?.onSeeInfo(record);
            }}
          >
            使用说明
          </a>
        );
      },
    },
    {
      title: '范本状态',
      dataIndex: 'stateName',
      key: 'stateName',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a?.state > b?.state ? 1 : -1),
      render: (text: string | null) => {
        const { color, colorText } = getColorTextLoadMap(text);
        return (
          <span className={pageStyles['over-flow-hidden']}>
            {/* <MyIcon type="icon-dian" style={{ color }} /> */}
            {colorText}
          </span>
        );
      },
    },
    // {
    //   title: '范本类型',
    //   dataIndex: 'templateTypeName',
    //   key: 'templateTypeName',
    //   width: 150,
    //   visible: true,
    //   sorter: (a: any, b: any) => (a?.templateType > b?.templateType ? 1 : -1),
    //   render: (text: string | null) => {
    //     return <span className={pageStyles['over-flow-hidden']}>{getColorTextLoadMapDom(text)}</span>;
    //   },
    // },
    {
      title: '合同类型',
      dataIndex: 'businessTypeName',
      key: 'businessTypeName',
      width: 150,
      visible: true,
      sorter: (a: any, b: any) => (a?.templateType > b?.templateType ? 1 : -1),
      render: (text: string | null) => {
        const { colorText } = getColorTextLoadMap(text);
        return <span className={pageStyles['over-flow-hidden']}>{colorText ? colorText : '--'}</span>;
      },
    },
    {
      title: '发布时间',
      dataIndex: 'signTime',
      key: 'signTime',
      visible: true,
      width: 130,
      sorter: (a: any, b: any) => (a.signTime > b.signTime ? 1 : -1),
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span className={pageStyles['over-flow-hidden']}>{dateText}</span>;
      },
    },
    {
      title: '发布人',
      dataIndex: 'signerName',
      key: 'signerName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => (a.signerID > b.signerID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布岗位',
      dataIndex: 'signPositionName',
      key: 'signPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.signPositionID > b.signPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布部门',
      dataIndex: 'signDepartmentName',
      key: 'signDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.signDepartmentID > b.signDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '发布公司',
    //   dataIndex: 'signCompanyName',
    //   key: 'signCompanyName',
    //   visible: false,
    //   width: 150,
    //   sorter: (a: any, b: any) => (a.signCompanyID > b.signCompanyID ? 1 : -1),
    //   render: (text: string | null) => {
    //     return (
    //       <Tooltip title={tooltipLength(text, 100)}>
    //         <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createTime > b.createTime ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => (a.createrID > b.createrID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createPositionID > b.createPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a.createDepartmentID > b.createDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '申请公司',
    //   dataIndex: 'createCompanyName',
    //   key: 'createCompanyName',
    //   visible: false,
    //   width: 150,
    //   sorter: (a: any, b: any) => (a.createCompanyID > b.createCompanyID ? 1 : -1),
    //   render: (text: string | null) => {
    //     return (
    //       <Tooltip title={tooltipLength(text, 100)}>
    //         <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '作废类型',
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => (a?.cancelType > b?.cancelType ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Fragment>
            <span className={pageStyles['over-flow-hidden']}>{getColorTextLoadMapDom(text)}</span>
          </Fragment>
        );
      },
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 180,
      sorter: (a: any, b: any) => (a?.cancelTime > b?.cancelTime ? 1 : -1),
      render: (text: string | null) => {
        return <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>;
      },
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 220,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 120,
      sorter: (a: any, b: any) => (a?.cancelerID > b?.cancelerID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '作废岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelPositionID > b?.cancelPositionID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '作废部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => (a?.cancelDepartmentID > b?.cancelDepartmentID ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={tooltipLength(text, 100)}>
            <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '作废公司',
    //   dataIndex: 'cancelCompanyName',
    //   key: 'cancelCompanyName',
    //   visible: false,
    //   width: 150,
    //   sorter: (a: any, b: any) => (a?.cancelCompanyID > b?.cancelCompanyID ? 1 : -1),
    //   render: (text: string | null) => {
    //     return (
    //       <Tooltip title={tooltipLength(text, 100)}>
    //         <span className={pageStyles['over-flow-hidden']}>{text ? text : '--'}</span>
    //       </Tooltip>
    //     );
    //   },
    // },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 220,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {Operation(curRes, clickFnc, record)}
          </div>
        );
      },
    },
  ];
};

export { getTableColumns };
